<?php
// 应用公共文件
/**
 * 获取分页
 * @param string $page
 * @param string $pageSize
 * @author dgc<453342213@qq.com>
 * createTime:2020/8/24 13:46
 */
function getPage($page = 'page', $pageSize = 'pageSize')
{
    return ['page' => request()->param($page, 1), 'list_rows' => request()->param($pageSize, 20)];
}

/**
 * 自定义JSON格式 默认带数据
 * @param null $data
 * @param string $msg
 * @param int $code
 * @author dgc<453342213@qq.com>
 * createTime:2020/8/24 13:48
 */
function returnDate($data = null, string $msg = 'success', int $code = 200)
{
    $res = [
        'code' => $code,
        'msg' => $msg,
        'data' => $data
    ];
    exit(json_encode($res));

}

/**
 * 自定义JSON格式 Code
 * @param string $msg
 * @param int $code
 * @param null $data
 * @author dgc<453342213@qq.com>
 * createTime:2020/8/24 13:48
 */
function returnMsg(string $msg = 'success', int $code = 200, $data = null)
{
    $res = [
        'code' => $code,
        'msg' => $msg,
        'data' => $data
    ];
    exit(json_encode($res, JSON_UNESCAPED_UNICODE));
}

/**
 * 获取客户端IP
 * @return mixed
 * @author dgc<453342213@qq.com>
 * createTime:2020/8/24 13:49
 */
function getClientIp()
{
    return request()->server('HTTP_ALI_CDN_REAL_IP', value(function () {
        $ip = request()->ip();

        if (request()->has('X_FORWARDED_FOR')) {
            $ips = [];

            foreach (explode(',', request()->header('X_FORWARDED_FOR')) as $v) {
                $ips[] = trim($v);
            }

            return head($ips) ?: $ip;
        }

        return $ip;
    }));
}

function head($array)
{
    return reset($array);
}

//----------- redis start ----------//

global $redisCon;
/**
 * redis
 * @return Redis
 * @author dgc<453342213@qq.com>
 * createTime:2020/8/24 13:49
 */
function redisConnect()
{
    if (empty($redisCon)) {
        $redis = new Redis();
        $redis->connect(env('redis.host','127.0.0.1'), env('redis.port',6379));
        if(env('redis.password','')!=''){
            $redis->auth(env('redis.password',''));
        }
        $redisCon = $redis;
    } else {
        $redis = $redisCon;
    }
    return $redis;
}

function redisHset($key, $hashKey, $value = "")
{
    $redis = redisConnect();
    return $redis->hSet($key, $hashKey, json_encode($value));

}

function redisHget($key, $hashKey)
{
    $redis = redisConnect();
    $data = $redis->hGet($key, $hashKey);
    return json_decode($data, true);
}

function redisHdel($key, $hasKey = null)
{
    if (is_null($hasKey)) {
        $hasKey = request()->header('token', '');
    }
    //删除缓存信息
    $redis = redisConnect();
    if ($hasKey) {
        $redis->hDel($key, $hasKey);
    }
}

// ----------  redis end --------------//
/**
 * 设置密码
 * @param $password
 * @param $salt
 * @return string
 * @author dgc<453342213@qq.com>
 * createTime:2020/8/24 13:49
 */
function setPassword($password, $salt)
{
    return md5(sha1($password . $salt));
}

/**
 * 指定Key 作为下标
 * @param $array
 * @param $arr_key
 * @author dgc<453342213@qq.com>
 * createTime:2020/8/31 18:00
 */
function convert_array_key($array, $arr_key)
{
    $arr = [];
    foreach ($array as $v) {
        $arr[$v[$arr_key] . ''] = $v;
        $arr[$v[$arr_key] . '']['is_unread'] = 0;
    }
    return $arr;
}

/**
 * [listToTreeMulti 格式化分类--生成多维数组 ，子数组放在child 属性中]
 * @param  [type]  $list  [数组]
 * @param integer $root [指定根节点]
 * @param string $pid [父级标识]
 * @param string $child [子级标识]
 * @return [type]         [description]
 */
function listToTree($list, $pid = 0, $pname = 'pid', $child = 'child', $pk = 'id')
{
    $tree = array();
    foreach ($list as $v) {
        if ($v[$pname] == $pid) {
            $v[$child] = listToTree($list, $v[$pk], $pname, $child, $pk);
            if (empty($v[$child])) {
                unset($v[$child]);
            }
            $v['hasChildren'] = true;
            $tree[] = $v;
        }
    }

    return $tree;
}
